package com.yufei.infoExtractor.util;
import java.io.File;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/*import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;*/

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jndi.JndiObjectTargetSource;

import com.yufei.infoExtractor.core.InfoExtractorConfig;
import com.yufei.infoExtractor.core.LinkAction;

import com.yufei.infoExtractor.core.LinkActionForMedia;
import com.yufei.infoExtractor.core.LinkActionForPagination;
import com.yufei.infoExtractor.core.LinkActionForUnknow;
import com.yufei.infoExtractor.pfw.impl.InfoExtractorDaoMongodImpl;

public class AppUtil {
private static Log mLog = LogFactory.getLog(AppUtil.class);

private static ApplicationContext appContext=null;
private static Properties crawlerProperties=null;
final static String defaultCrawlerDateFilePath="conf/infoExtractorConfig.xml";
final static String defaultSpringConfigPath="conf/app.xml";
final static String cacheConf="conf/cache.xml";
public final static String defaultUrlCache="UrlCache";
static InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream(AppUtil.cacheConf);





/*public static Object get(ServletContext app,String bean){
	
	return ((BeanFactory)app.getAttribute("beanFactory")).getBean(bean);
	
	
}*/
//BeanFactory beanFactory = new ClassPathXmlApplicationContext("com/fz/life/core/config/BeanContainer.xml");

//init linkaction

public static Object getBeanFromBeanContainer(String name){
	return getAppContext().getBean(name);
	
}
public static Object getBeanFromBeanContainer(Class c){
	return getAppContext().getBean(c);
	
}//初始化各种类型连接处理对象
public static  Map<String,LinkAction> linkActions=new HashMap();
static{


linkActions.put(String.valueOf(LinkAction.LINKTYPE_UNKNOW), new LinkActionForUnknow());
linkActions.put(String.valueOf(LinkAction.LINKTYPE_PAGINATION), new LinkActionForPagination());
linkActions.put(String.valueOf(LinkAction.LINKTYPE_MEDIAATION), new LinkActionForMedia());

}
public static Object getEjb(String name){
	return ((JndiObjectTargetSource)getAppContext().getBean(name)).getTarget();
	
}

private static ApplicationContext getAppContext(){
	appContext=appContext== null?new ClassPathXmlApplicationContext(defaultSpringConfigPath):appContext;
	return appContext;
	
	
}
private static Properties getProperties(String path){
	Properties properties=new Properties();
	
	InputStream in=null;
	try {
		URL resource=Thread.currentThread().getContextClassLoader().getResource(path);
        
		
		properties.load(resource.openStream());
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return properties;
	
}
@Deprecated
public static String getCrawlerProperty(String key){
	/*if(crawlerProperties==null){
		crawlerProperties=getProperties(AppConstants.CRAWLER_PROPERTIES);
	}
	return crawlerProperties.getProperty(key);*/
	return null;
	
} 



public static InfoExtractorConfig  getInfoExtractorConfig(){
	InfoExtractorConfig crawlerConfig=null;
	//URL resource=Thread.currentThread().getContextClassLoader().getResource(AppUtil.defaultCrawlerDateFilePath);//new ClassPathResource(AppUtil.defaultCrawlerDateFilePath);
	InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream(AppUtil.defaultCrawlerDateFilePath);
	
	JAXBContext jaxbContext;
	try {
		jaxbContext = JAXBContext.newInstance(InfoExtractorConfig.class);
		Unmarshaller unmarshaller=jaxbContext.createUnmarshaller();
	    crawlerConfig=(InfoExtractorConfig) unmarshaller.unmarshal(in);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		mLog.error("reading CrawlerConfig wrong!");
		throw new RuntimeException(e);
	} 
	
	return crawlerConfig;
	
}

public static LinkAction getLinkAction(String linkType){
	LinkAction linkAction=linkActions.get(linkType);
	if(linkAction==null){
		throw new IllegalArgumentException("请输入有效的链接类型！");
	}
	return linkAction;
	
}
/*public static  Cache getCacheByCacheName(String cacheName){
	Cache cache=null;
	cache=cacheManager.getCache(cacheName);
	if(cache==null){
		cacheManager.addCache(cacheName);
		cache=cacheManager.getCache(cacheName);
	}
	
   
	return cache;
	
}*/
































































































}
